package push2java;

import java.util.EmptyStackException;

/**
 * Some simple tests to show executing compiled programs is faster than
 * interpreted ones, and that the compiler produces code that works as expected.
 * 
 * @author James Pettit
 */
public class Test {

    public static void main(String[] args) throws Exception {
        Evolution evolver = new Evolution(new FitnessFunction() {

            public float fitness(PushProgram program) {
                try {
                    float result = (Float) program.evaluate(1, 2, 3, 4, 5).floatStack.peek();
                    float error = Math.abs(result - (3 + 4 + 5));
                    if (error == 0) {
                        return Float.POSITIVE_INFINITY;
                    } else {
                        return 1 / error;
                    }
                } catch (EmptyStackException ex) {
                    return Float.NEGATIVE_INFINITY;
                }
            }
        });
        while (!evolver.finished()) {
            evolver.step();
            System.err.println(evolver.getBest().fitness);
        }
    }
}
